package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DownOrUp {
    @Autowired
    private ApArticleConfigService apArticleConfigService;


    @KafkaListener(topics = "down_or_up")
    public void getMsg(String msg) {
        Map map = JSON.parseObject(msg, Map.class);
        Long articleId = (Long) map.get("articleId");
        Integer enable = (Integer) map.get("enable");

        apArticleConfigService.update(
                Wrappers.<ApArticleConfig>lambdaUpdate()
                        .eq(ApArticleConfig::getArticleId, articleId)
                        .set(ApArticleConfig::getIsDown, enable == 1 ? false : true)
        );
    }
}
